package com.example.administrator.zhongyichou.utils;

import android.os.Environment;

import java.io.File;
import java.io.IOException;

/**
 * Created by lichenyu on 2017/11/27.
 * 描述：
 */

public class FileUtils {

    public final static String projectResoucePath = "/gongyichou/";
    public final static String sdPath = Environment.getExternalStorageDirectory().toString();
    public final static String dataPath = "/data/data/com.example.administrator.zhongyichou/";

    /**
     * 创建文件
     *
     * @param path     文件路径
     * @param filename 文件名
     * @return
     */
    public static File creatFilePath(String path, String filename) {
        boolean flag = creatFilePath(path);
        File file = new File(path, filename);
        if (file.exists()) {
            file.delete();
        }
        if (flag) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }

    /**
     * 创建文件夹
     *
     * @param Path 文件夹路径
     * @return
     */
    public static boolean creatFilePath(String Path) {
        File path = new File(Path);
        if (!path.exists()) {
            return path.mkdirs();
        }
        return true;
    }

    /**
     * 获取手机app文件目录
     *
     * @return
     */
    public static String getProjectFilePath() {
        return getFilePath() + projectResoucePath;
    }

    /**
     * 获取手机app文件图片目录
     *
     * @return
     */
    public static String getProjectFileImagePath() {
        return getFilePath() + projectResoucePath + "img/";
    }

    /**
     * 获取手机根目录
     *
     * @return
     */
    public static String getFilePath() {
        if (hasSdcard()) {
            return sdPath;

        } else {
            return dataPath;
        }
    }

    /**
     * 是否存在sd卡
     *
     * @return
     */
    public static boolean hasSdcard() {
        String status = Environment.getExternalStorageState();
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean clearImage() {

        return delAllFile(getProjectFileImagePath());
    }

    //删除指定文件夹下所有文件
//param path 文件夹完整绝对路径
    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
                delFolder(path + "/" + tempList[i]);//再删除空文件夹
                flag = true;
            }
        }
        return flag;
    }

    //删除文件夹
//param folderPath 文件夹完整绝对路径

    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); //删除完里面所有内容
            String filePath = folderPath;
            filePath = filePath.toString();
            java.io.File myFilePath = new java.io.File(filePath);
            myFilePath.delete(); //删除空文件夹
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
